package com.wnhz.work.yz.fragment.project;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.JobauditActivity;
import com.wnhz.work.yz.adapter.JobItemAdapter;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.bean.JobProjectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class JobAllFragment extends Fragment implements BaseRVAdapter.OnItemClickListener {
    private View view;
    private JobauditActivity activity;

    public String getType() {
        return type;
    }

    private String type;
    private ListView my_listView;
    private Context context;
    private RecyclerView recycler;

    public void setContext(Context context) {
        this.context = context;
    }


    public void setType(String type) {
        this.type = type;
    }


    public JobAllFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_job_all, container, false);
        // activity = (JobauditActivity) getActivity();
        initView(view);
        upload("0");
        return view;
    }


    private void initData() {
        if (meetingData == null) {
            return;
        }
        final List<JobProjectBean.InfoBean> info = meetingData.getInfo();
        //  Toast.makeText(context, "JobAllFragment:" + info.size(), Toast.LENGTH_SHORT).show();
        my_listView.setAdapter(new JobItemAdapter(context, info));
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        recycler.setLayoutManager(linearLayoutManager);
//        recycler.setAdapter(new JobItemAdapter(context, info));

//        my_listView.setFocusable(true);
//        my_listView.setFocusableInTouchMode(true);
//        //  Log.e("----","1111");
//        my_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
        // Intent intent = new Intent((JobauditActivity) context, JobTepiDetailsActivity.class);
////                ArrayList<JobProjectBean.InfoBean> info2 = (ArrayList<JobProjectBean.InfoBean>) info;
////                intent.putExtra("bean", info2);
////                startActivity(intent);
//            }
//        });
    }

    private void initView(View view) {
        my_listView = (ListView) view.findViewById(R.id.my_listView);
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
    }

    JobProjectBean meetingData;

    public void upload(String type) {
        //  showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);

        XUtil.Post(Url.ORDER_GETOSHENPINGTEPI, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtil.i(result);
//                Log.i(TAG, "onSuccess: AllOrderActivity= " +result.substring(0, result.length() / 2));
//                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring(result.length() / 2, (result.length() / 2 * 1)));
//                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring((result.length() / 2 * 1), result.length()));
                //  JSONObject jsonObject = new JSONObject(result);
                //  String res = jsonObject.optString("result");
                // JSONArray jsonArray = jsonObject.optJSONArray("info");

                Gson gson = new Gson();
                meetingData = gson.fromJson(result, JobProjectBean.class);

            }

            @Override
            public void onFinished() {
                //    closeDialog();
                initData();
                handler.sendEmptyMessage(1);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //  closeDialog();
                Log.e("-----", "tyuyuty");
                super.onError(ex, isOnCallback);
                //  Log.e(TAG, "onError: " + ex);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initData();
        }
    };

    @Override
    public void onDestroy() {
        if (handler != null)
            handler.removeCallbacks(null);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        upload("0");
        super.onResume();
    }

    @Override
    public void itemSelect(int position) {
        Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
    }
}
